package cmm;
import cmm.Lexer.*;

import java.io.*;
import java.util.LinkedList;

import cmm.Lexer.Token;
import cmm.Parser.Parser;
import cmm.Parser.TreeNode;
import cmm.Seman.FourCode;
import cmm.Seman.Seman;

/**
 * Created by zhangzheyu on 2017/10/10.
 */
public class Test {
    public static void main(String []args) throws Exception {
        if (args.length == 1){
            StringBuffer buffer =getFileContent(args[0]);
            Lexer lexer =new Lexer(new BufferedReader(new StringReader(buffer.toString())));


//            try{
                lexer.lex();
                Parser parser = new Parser();
                parser.parse(new LinkedList<Token>(lexer.tokens));
                parser.printtoTree();
//            System.out.println(parser.getTreeNodes().size());
            for (TreeNode fc : parser.getTreeNodes()) {
                System.out.println(fc.toString());
            }
//            for (Token t : lexer.getTokens()) {
//                System.out.println(t.getValue() + " - " + t.getType());
//            }
            System.out.println(lexer.getTokens());
                Seman seman = new Seman();
                LinkedList<FourCode> codes= seman.generateCode(parser);
                for (int i = 0; i < codes.size(); i++) {
                    System.out.println( i + " :  " + codes.get(i).toString());
                }
//                Executor executor = new Executor(codes);
//            }catch (Exception e){
//                System.out.println(e);
//            }





        }
    }
    public  static  StringBuffer getFileContent(String  filename)throws IOException{
        FileInputStream inputStream = new FileInputStream(filename);
        StringBuffer buffer =new StringBuffer();
        String line = "";

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        line =bufferedReader.readLine();
        while(line !=null){
            buffer.append(line);
            buffer.append("\n");
            line = bufferedReader.readLine();
        }
        inputStream.close();
        return buffer;
    }
}
